package hb.software.attendance.entitygenerator;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;

import hb.software.attendance.domain.AttendanceRecord;
import hb.software.attendance.service.AttendanceRecordManager;
import hb.software.service.GenericManagerTestCase;


/** 
 * @ClassName: AttendanceRecordEntityGenerator 
 * @Description: TODO
 * @author WangPeiDong
 * @date May 27, 2020 9:16:19 PM 
 *  
 */
@ContextConfiguration(locations = { "classpath:applicationContextTest-resources.xml",
		"classpath:applicationContext-dao.xml", "classpath:applicationContext-service.xml" })
public class AttendanceRecordEntityGenerator extends AbstractJUnit4SpringContextTests{
	List<AttendanceRecord> attendanceRecordList;

	AttendanceRecordManager attendanceRecordManager;
	@Autowired
	public void setattendanceRecordManager(AttendanceRecordManager attendanceRecordManager) {
		this.attendanceRecordManager = attendanceRecordManager;
	}
	
	
	@Before
	public void setUp() throws Exception {
		this.attendanceRecordList = new ArrayList<AttendanceRecord>();
		InputStream input = GenericManagerTestCase.class.getResourceAsStream("/javaee_attendanceRecord_software.xlsx");
		@SuppressWarnings("resource")
		XSSFWorkbook wb = new XSSFWorkbook(input);
		XSSFSheet sheet = wb.getSheetAt(0);
		for (int i = 1; i <= sheet.getLastRowNum(); i++) {
			Row row = sheet.getRow(i);
			AttendanceRecord s = new AttendanceRecord();
			for (int j = 0; j < row.getLastCellNum(); j++) {
				Cell cell = row.getCell(j);
				if (cell != null) {
					cell.setCellType(CellType.STRING);
					String value = row.getCell(j).getStringCellValue().trim();
					if (j == 1)
						s.setBeginTime(value);
					if (j == 2)
						s.setEndTime(value);
					if (j == 3)
						s.setState(value);
					if (j == 4)
						s.setName(value);
						}
				}
			this.attendanceRecordList.add(s);
			}
	
			
		}
	@Test
	public void test() {
		this.attendanceRecordManager.deleteAll();
		this.attendanceRecordManager.save(this.attendanceRecordList);
		Iterable<AttendanceRecord> result = this.attendanceRecordManager.findAll();
		if (logger.isInfoEnabled()) {
			logger.info("test() - List<attendanceRecord> result=" + result); //$NON-NLS-1$
		}
	}
	
	
	
}
